/*
 * @Author: lihaogn
 * @Date: 2021-11-14 09:32:40
 * @LastEditTime: 2022-05-27 14:33:13
 * @LastEditor: lihaogn
 * @Description: 用户 - controller
 * @FilePath: \lix-admin-server\app\controller\user.js
 */

const Controller = require("egg").Controller;
const { SuccessResponse, ErrorResponse } = require("../dto/responseDto");

class UserController extends Controller {
  /**
   * @description: 获取用户列表数据
   * @param {*}
   * @return {*}
   */
  async index() {
    const { ctx } = this;

    if (Object.keys(ctx.query).length !== 0) {
      // 根据条件查询
      if (ctx.query.name) {
        // 根据用户名查询
        const res = await ctx.service.user.queryUserByName(ctx.query.name);

        if (res === false) {
          ctx.body = new ErrorResponse(null, "服务端异常");
        } else {
          if (res === null) {
            ctx.body = new SuccessResponse({ isExists: false }, "用户名可用");
          } else {
            ctx.body = new SuccessResponse({ isExists: true }, "用户名已存在");
          }
        }
      } else if (ctx.query.page) {
        // 分页查询
        const res = await ctx.service.user.queryUsersByPage(
          ctx.query.page,
          ctx.query.size
        );
        if (res === false) {
          ctx.body = new ErrorResponse(null, "服务端异常");
        } else {
          ctx.body = new SuccessResponse(res, "");
        }
      }
    } else {
      // 获取所有用户信息
      const res = await ctx.service.user.queryUsers();
      if (res === false) {
        ctx.body = new ErrorResponse(null, "获取数据失败");
      } else {
        ctx.body = new SuccessResponse(res, "");
      }
    }
  }

  /**
   * @author: lihaogn
   * @Date: 2021-11-18 20:30:07
   * @description: 根据 id 查询用户信息
   * @param {*}
   * @return {*}
   */
  async show() {
    const { ctx } = this;

    const res = await ctx.service.user.queryUserById(ctx.params.id);

    if (res === false) {
      ctx.body = new ErrorResponse(null, "获取用户数据失败");
    } else {
      ctx.body = new SuccessResponse(res, "");
    }
  }

  /**
   * @description: 修改用户信息
   * @param {*}
   * @return {*}
   */
  async update() {
    const { ctx, app } = this;
    const nsp = app.io.of("/");

    let res;
    const reqBody = ctx.request.body;
    if (reqBody.type === "EDIT") {
      // 修改
      res = await ctx.service.user.update(ctx.params.id, reqBody);
    } else if (reqBody.type === "editPassword") {
      // 重置密码
      res = await ctx.service.user.editPassword(ctx.params.id, reqBody);
    } else if (reqBody.type === "editStatus") {
      // 设置用户状态
      res = await ctx.service.user.editStatus(ctx.params.id, reqBody);
      if (res) {
        if (reqBody.userStatus === 2 || reqBody.userStatus === 3) {
          nsp.emit("logout", { msg: "logout", id: ctx.params.id });
        }
      }
    }

    if (res === false) {
      ctx.body = new ErrorResponse(null, "修改失败");
    } else {
      ctx.body = new SuccessResponse(null, "修改成功");
    }
  }

  /**
   * 新增用户
   * @returns {Promise<void>}
   */
  async create() {
    const { ctx } = this;
    const res = await ctx.service.user.createUser(ctx.request.body);
    if (res === false) {
      ctx.body = new ErrorResponse(null, "新增失败");
    } else {
      ctx.body = new SuccessResponse(null, "新增成功");
    }
  }

  /**
   * @author: lihaogn
   * @Date: 2021-12-11 18:14:00
   * @description: 导入用户
   * @param {*}
   * @return {*}
   */
  async import() {
    const { ctx, app } = this;
    const res = await ctx.service.user.importUser(ctx.request.body);
    console.log("import res", res);
    if (res === true) {
      ctx.body = new SuccessResponse(null, "导入成功");
    } else {
      ctx.body = new ErrorResponse(null, res.message);
    }
  }

  /**
   * @Author: lihaogn
   * @Date: 2021-08-14 09:04:39
   * @Description: 删除一个用户
   * @Param:
   * @Return:
   */
  async destroy() {
    const { ctx, app } = this;
    const nsp = app.io.of("/");
    const res = await ctx.service.user.deleteOneUser(ctx.params.id);

    if (res === false) {
      ctx.body = new ErrorResponse(null, "删除失败");
    } else {
      ctx.body = new SuccessResponse(null, "删除成功");
      nsp.emit("logout", { msg: "logout", id: ctx.params.id });
    }
  }
}

module.exports = UserController;
